package com.example.libraryapp.util;

import com.example.libraryapp.user.UserInfo;

import android.content.Context;
import android.content.SharedPreferences;

public class MyConfig {
	
	/**
     * 是否创建数据库
     * @param context
     */
    public static void setDbCreated(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("createdb", Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("flag", true);
        //提交数据
        editor.commit();
    }
    
    /**
     * 是否创建过数据库
     * @param context
     */
    public static boolean getDbCreated(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("createdb", Context.MODE_PRIVATE);
        return preferences.getBoolean("flag", false);
    }
	
	 /**
     * 设置用户信息
     * @param context
     */
    public static void setUserInfo(Context context, UserInfo userInfo) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        if (userInfo != null) {
            editor.putInt("urid", userInfo.getUrid());
            editor.putString("cardid", userInfo.getCardid());
            editor.putString("username", userInfo.getUsername());
        } else{
            editor.putInt("urid", 0);
            editor.putString("cardid", "");
            editor.putString("username", "");
        }
        //提交数据
        editor.commit();

    }
    /**
     * 得到用户信息
     * @param context
     */
    public static UserInfo getUserInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        if (preferences != null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUrid(preferences.getInt("urid",0));
            if (Validator.isEffective(preferences.getString("cardid", ""))) {
                userInfo.setCardid(preferences.getString("cardid", ""));
            }
            if (Validator.isEffective(preferences.getString("username", ""))) {
                userInfo.setUsername(preferences.getString("username", ""));
            }
            return userInfo;
        }
        return null;
    }
}
